package zz.demo.springCloud.wechat.config;

import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zz.demo.springCloud.wechat.entity.WeChatApp;
import zz.demo.springCloud.wechat.service.WeChatAppService;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WxCpConfiguration {

    @Autowired
    private WeChatAppService weChatAppService;

    @Bean
    @ConditionalOnMissingBean
    public List<WxCpServiceAdapter> wxCpServiceList() throws WxErrorException{
        List<WeChatApp> weChatAppList = weChatAppService.findAllAndCheck();
        List<WxCpServiceAdapter> wxCpServiceList = new ArrayList<WxCpServiceAdapter>();
        for(WeChatApp weChatApp:weChatAppList){
            wxCpServiceList.add(this.wxCpService().build(weChatApp));
        }
        return wxCpServiceList;
    }

    @Bean
    public WxCpServiceAdapter wxCpService(){
        return new WxCpServiceAdapter();
    }

}
